package com.jacobson.huobi.mvp

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.alibaba.fastjson.JSONObject
import com.jacobson.core.base.Immersive
import com.jacobson.core.di.components.AppComponent
import com.jacobson.core.di.modules.ActivityModule
import com.jacobson.core.mvp.BaseMvpActivity
import com.jacobson.huobi.Constants
import com.jacobson.huobi.R
import com.jacobson.huobi.di.components.DaggerForgetActivityComponent
import com.jacobson.huobi.di.modules.ForgetActivityModule
import com.jacobson.huobi.entities.ForgetInfo
import com.jacobson.huobi.entities.response.CommonResponse
import com.jacobson.huobi.entities.response.LoginPageResponse
import com.jacobson.huobi.mvp.contract.ForgetContract
import com.jacobson.huobi.utils.DataUtil
import com.jacobson.huobi.utils.EncryptUtil
import com.jacobson.huobi.utils.ResourcesUtil
import com.jacobson.huobi.utils.VertiCodeTimer
import kotlinx.android.synthetic.main.activity_login.btnLoginAndRegister
import kotlinx.android.synthetic.main.activity_login.btnRefreshVertiCode
import kotlinx.android.synthetic.main.activity_login.etPassword
import kotlinx.android.synthetic.main.activity_login.etPhoneNumber
import kotlinx.android.synthetic.main.activity_login.etVertiCode
import kotlinx.android.synthetic.main.forget_activity.*
import java.util.regex.Pattern

@Immersive(fitSystemWindows = false)
class ForgetActivity : BaseMvpActivity<ForgetContract.Presenter>(), ForgetContract.View {
    private lateinit var timer: VertiCodeTimer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //修改底部功能键背景色 只支持api为21及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor =
                ResourcesUtil.getColor(R.color.colorPrimary) //设定底部导航栏  同全局颜色
        }
    }

    override fun getVertiCode(response: LoginPageResponse) {
        if (response.code == Constants.RESP_OK) {
            Toast.makeText(this,"发送短信成功",Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, response.msg, Toast.LENGTH_SHORT).show()
        }
    }

    override fun forgetUser(response: CommonResponse<JSONObject>) {
        if (response.code == Constants.RESP_OK) {
            Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show()
            finish()
        }else {
            Toast.makeText(this, "密码修改失败,请联系管理员", Toast.LENGTH_SHORT).show()
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerForgetActivityComponent.builder()
            .forgetActivityModule(ForgetActivityModule(this))
            .appComponent(appComponent)
            .activityModule(ActivityModule(this))
            .build()
            .inject(this)
    }

    override fun initView(savedInstanceState: Bundle?): Int = R.layout.forget_activity

    override fun initData(savedInstanceState: Bundle?) {
        timer = VertiCodeTimer(60000, 1000, btnRefreshVertiCode)
        initClick()
    }

    private fun initClick() {

        //刷新手机号验证码
        btnRefreshVertiCode.setOnClickListener {
            val phoneNumber = etPhoneNumber.text.trim().toString()
            if (phoneNumber.isBlank()) {
                Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show()
            } else {
                if (checkPhoneNum(phoneNumber)) {
                    presenter.getVertiCode(phoneNumber)
                    timer.start()
                } else {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show()
                }

            }
        }
        btnLoginAndRegister.setOnClickListener {
            forgetUser()
        }
        btnBack.setOnClickListener {
            finish()
        }
    }
    /***
     * 手机号码检测
     */
    private fun checkPhoneNum(num: String): Boolean {
        val regExp = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(14[5-9])|(166)|(19[8,9])|)\\d{8}$"
        val p = Pattern.compile(regExp)
        val m = p.matcher(num)
        return m.matches()
    }

    /**
     * 忘记密码
     */
    private fun forgetUser() {
        var password = etPassword.text.trim().toString()
        var phoneNumber = etPhoneNumber.text.trim().toString()
        var vertiCode = etVertiCode.text.trim().toString()

        if (phoneNumber.isNullOrBlank()) {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show()
            return
        }
        if (password.isNullOrBlank()) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show()
            return
        }
        if (!DataUtil.isLetterDigit(password) || password.length < 8) {
            Toast.makeText(this, "密码至少8位且必须包含英文和数字", Toast.LENGTH_SHORT).show()
            return
        }

        if (vertiCode.isNullOrBlank()) {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show()
            return
        }

        password = EncryptUtil.encrypData(password , vertiCode)
        if (password.isNullOrBlank()) {
            Toast.makeText(this, "密码加密出错,请联系管理员", Toast.LENGTH_SHORT).show()
            return
        }
        var forgetInfo = ForgetInfo( password,
            phoneNumber, vertiCode)
        presenter.forgetUser(forgetInfo)
    }
    companion object {
        fun startActivity(context: Context) {
            Intent(context, ForgetActivity::class.java).run {
                context.startActivity(this)
            }
        }
    }

}